package com.zlp.mall.search.controller;


import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.search.domain.EsSpu;
import com.zlp.mall.search.domain.Googs3;
import com.zlp.mall.search.service.EsGoodsService;
import com.zlp.mall.search.service.EsSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 搜索App商品管理 Controller
 * @since 2022年5月16日15:41:36
 */
@RestController
@Api(value = "EsGoodsController", tags = "搜索EsGoods商品管理")
@RequestMapping("/EsGoods")
public class EsGoodsController {

    @Resource
    private EsGoodsService esGoodsService;

    @Resource
    private  ElasticsearchRestTemplate elasticsearchRestTemplate;


    @GetMapping("createIndex")
    @ApiOperation(value = "创建 Googs3 文档对象")
    public boolean createIndex() {
        boolean result = elasticsearchRestTemplate.createIndex(Googs3.class);
        return result;
    }

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @PostMapping(value = "/importAll")
    public R<Integer> importAllList() {
        int count = esGoodsService.importAll(null);
        return R.success(count);
    }





    @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @GetMapping(value = "/search")
    public R<Pager<Googs3>> search(@RequestParam(required = false) String keyword,
                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                      @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Page<Googs3> esProductPage = esGoodsService.search(keyword,  pageNum, pageSize, sort);
        return R.success(Pager.restPage(esProductPage));
    }



}
